#include <algorithm>
#include <cassert>
#include <nice2d/piece/piecerandomsorter.h>
#include <nice2d/piece/piecevisitor.h>

IMPLEMENT_OBJECT(PieceRandomSorter,PieceSorter)

PieceRandomSorter::PieceRandomSorter(NodePointer object):
    PieceSorter(object)
{
}

PieceRandomSorter::~PieceRandomSorter()
{
}

void PieceRandomSorter::sort(std::shared_ptr<PieceScene> scene, const sf::Color &color)
{
    assert(scene && "invalid scene pointer");

    auto visitor = std::make_shared<SpecifiedColorPieceVisitor>(color);
    scene->acceptPieces(visitor);

    auto pieces = visitor->getPieces();
    std::random_shuffle(pieces.begin(),pieces.end());

    std::for_each(pieces.begin(),pieces.end(),[&](std::shared_ptr<Piece> piece){addPiece(piece);});
}
